package com.sgb.kjwl.view.ui.fragment.packers;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentOtherAskBinding;
import com.sgb.kjwl.model.entity.packers.OtherAskTransmitEntity;

import org.jetbrains.annotations.NotNull;

/**
 * @author : younghow
 * @date : 2021/7/22 16:15
 * description: 展示从主要界面的传递过来的数据，这个界面也可能不会有，因为可能这些数据统统不存在
 * first:写在前面，请注意，我们在写代码的时候对于fragment和activity的全局变量的管理，这个管理需要我们做的是将可以在方法内部写入的变量请不要写在
 * 全局，我们都知道方法的变量会随着方法的调用结束而被销毁，然而全局是随着类的销毁而销毁，这和我们设计的初衷不符，如果你了解过c或者c++你会知道全局
 * 的变量其实会在当前的界面或者类里面进行空间的开辟。当这个类或者界面一直存在，那其实这样的全局会造成一定程度的泄露
 * second:第二点，在这里做的需求我都在一些我自己写的很辣鸡或者说需求本身是这样的很奇怪或者我自己认为很奇怪得地方做了注释，不一定很详尽，如果你需要
 * 修改我的原有代码。希望你能认真看完，谢谢
 */
public class OtherAskFragment extends AppFragment {

    /**
     * 这是这个界面的dataBinding
     */
    FragmentOtherAskBinding mBinding;
    /**
     * 这个对象是我们从主要界面传过来的，用来更新数据，此界面只有展示功能，不具备编辑功能，所以这是个单向传输界面
     */
    private OtherAskTransmitEntity otherAskTransmitEntity;

    public static OtherAskFragment newInstance(OtherAskTransmitEntity mEntity) {
        OtherAskFragment otherAskFragment = new OtherAskFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("entity", mEntity);
        otherAskFragment.setArguments(bundle);
        return otherAskFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_other_ask, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        otherAskTransmitEntity = (OtherAskTransmitEntity) getArguments().getSerializable("entity");

        initData();
    }

    /**
     * 这个地方我们采取的是最原始的方式来展示界面，分成了三组，第一组是以第一条分割线为基准的上面的参数，包含第一个if里面的所有参数，当这些参数全部为空
     * 或者为空字符的时候，我们采取的是隐藏对应字段及其分割线，下面同理，这里还涉及到一个问题就是最下面有三个自定义字段，这三个自定义字段产生的集合来
     * 确定控件显示和隐藏，在本方法的最后一个if
     */
    private void initData() {
        if (TextUtils.isEmpty(otherAskTransmitEntity.getConditionsOfResidence()) && TextUtils.isEmpty(otherAskTransmitEntity.getEatingConditions())
                && TextUtils.isEmpty(otherAskTransmitEntity.getSalaryManagementName()) && TextUtils.isEmpty(otherAskTransmitEntity.getPayrollPaymentName())
                && TextUtils.isEmpty(otherAskTransmitEntity.getProfessionalClothing())) {
            mBinding.llOtherAskConditionsOfResidence.setVisibility(View.GONE);
            mBinding.llOtherAskEatingConditions.setVisibility(View.GONE);
            mBinding.llOtherAskSalaryManagement.setVisibility(View.GONE);
            mBinding.llOtherAskPayrollPayment.setVisibility(View.GONE);
            mBinding.llOtherAskProfessionalClothing.setVisibility(View.GONE);
        } else {
            if (!TextUtils.isEmpty(otherAskTransmitEntity.getConditionsOfResidence())) {
                mBinding.tvOtherAskConditionsOfResidence.setText(otherAskTransmitEntity.getConditionsOfResidence());
            } else {
                mBinding.llOtherAskConditionsOfResidence.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(otherAskTransmitEntity.getEatingConditions())) {
                mBinding.tvOtherAskEatingConditions.setText(otherAskTransmitEntity.getEatingConditions());
            } else {
                mBinding.llOtherAskEatingConditions.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(otherAskTransmitEntity.getSalaryManagementName())) {
                mBinding.tvOtherAskSalaryManagement.setText(otherAskTransmitEntity.getSalaryManagementName());
            } else {
                mBinding.llOtherAskSalaryManagement.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(otherAskTransmitEntity.getPayrollPaymentName())) {
                mBinding.tvOtherAskPayrollPayment.setText(otherAskTransmitEntity.getPayrollPaymentName());
            } else {
                mBinding.llOtherAskPayrollPayment.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(otherAskTransmitEntity.getProfessionalClothing())) {
                mBinding.tvOtherAskProfessionalClothing.setText(otherAskTransmitEntity.getProfessionalClothing());
            } else {
                mBinding.llOtherAskProfessionalClothing.setVisibility(View.GONE);
            }
        }


        if (TextUtils.isEmpty(otherAskTransmitEntity.getAgeRequirementsName()) && TextUtils.isEmpty(otherAskTransmitEntity.getSexRequirementsName())
                && TextUtils.isEmpty(otherAskTransmitEntity.getEthnicRequirementsName()) && TextUtils.isEmpty(otherAskTransmitEntity.getLengthOfServiceName())
                && TextUtils.isEmpty(otherAskTransmitEntity.getMedicalCertificateName()) && TextUtils.isEmpty(otherAskTransmitEntity.getPerformanceRequirements())) {
            mBinding.ivOtherAskTagOne.setVisibility(View.GONE);
            mBinding.llOtherAskAgeRequirements.setVisibility(View.GONE);
            mBinding.llOtherAskGenderRequirements.setVisibility(View.GONE);
            mBinding.llOtherAskEthnicRequirements.setVisibility(View.GONE);
            mBinding.llOtherAskServiceServiceRequirements.setVisibility(View.GONE);
            mBinding.llOtherAskMedicalCertificate.setVisibility(View.GONE);
            mBinding.llOtherAskPerformanceRequirements.setVisibility(View.GONE);
        } else {
            if (!TextUtils.isEmpty(otherAskTransmitEntity.getAgeRequirementsName())) {
                mBinding.tvOtherAskAgeRequirements.setText(otherAskTransmitEntity.getAgeRequirementsName());
            } else {
                mBinding.llOtherAskAgeRequirements.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(otherAskTransmitEntity.getSexRequirementsName())) {
                mBinding.tvOtherAskGenderRequirements.setText(otherAskTransmitEntity.getSexRequirementsName());
            } else {
                mBinding.llOtherAskGenderRequirements.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(otherAskTransmitEntity.getEthnicRequirementsName())) {
                mBinding.tvOtherAskEthnicRequirements.setText(otherAskTransmitEntity.getEthnicRequirementsName());
            } else {
                mBinding.llOtherAskEthnicRequirements.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(otherAskTransmitEntity.getLengthOfServiceName())) {
                mBinding.tvOtherAskServiceServiceRequirements.setText(otherAskTransmitEntity.getLengthOfServiceName());
            } else {
                mBinding.llOtherAskServiceServiceRequirements.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(otherAskTransmitEntity.getMedicalCertificateName())) {
                mBinding.tvOtherAskMedicalCertificate.setText(otherAskTransmitEntity.getMedicalCertificateName());
            } else {
                mBinding.llOtherAskMedicalCertificate.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(otherAskTransmitEntity.getPerformanceRequirements())) {
                mBinding.tvOtherAskPerformanceRequirements.setText(otherAskTransmitEntity.getPerformanceRequirements());
            } else {
                mBinding.llOtherAskPerformanceRequirements.setVisibility(View.GONE);
            }
        }

        if (otherAskTransmitEntity.getAddField() != null){
            for (int i = 0; i < otherAskTransmitEntity.getAddField().size(); i++) {
                if (i == 0){
                    mBinding.ivOtherAskTagTwo.setVisibility(View.VISIBLE);
                    mBinding.tvOtherAskAddFieldTitleOne.setVisibility(View.VISIBLE);
                    mBinding.tvOtherAskAddFieldContentOne.setVisibility(View.VISIBLE);
                    mBinding.tvOtherAskAddFieldTitleOne.setText(otherAskTransmitEntity.getAddField().get(i).getName());
                    mBinding.tvOtherAskAddFieldContentOne.setText(otherAskTransmitEntity.getAddField().get(i).getContent());
                }

                if (i == 1){
                    mBinding.tvOtherAskAddFieldTitleTwo.setVisibility(View.VISIBLE);
                    mBinding.tvOtherAskAddFieldContentTwo.setVisibility(View.VISIBLE);
                    mBinding.ivOtherAskTagThree.setVisibility(View.VISIBLE);
                    mBinding.tvOtherAskAddFieldTitleTwo.setText(otherAskTransmitEntity.getAddField().get(i).getName());
                    mBinding.tvOtherAskAddFieldContentTwo.setText(otherAskTransmitEntity.getAddField().get(i).getContent());
                }

                if (i == 2){
                    mBinding.tvOtherAskAddFieldTitleThree.setVisibility(View.VISIBLE);
                    mBinding.tvOtherAskAddFieldContentThree.setVisibility(View.VISIBLE);
                    mBinding.ivOtherAskTagFour.setVisibility(View.VISIBLE);
                    mBinding.tvOtherAskAddFieldTitleThree.setText(otherAskTransmitEntity.getAddField().get(i).getName());
                    mBinding.tvOtherAskAddFieldContentThree.setText(otherAskTransmitEntity.getAddField().get(i).getContent());
                }
            }
        }
    }
}
